﻿using DingVision.Axis.Models;
using DingVision.Infrastructure.Events;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DingVision.Axis.ViewModels
{
    public class CurrentPosViewModel : BindableBase
    {
        #region 属性绑定
        private double pos_Axis1;
        public double Pos_Axis1
        {
            get { return pos_Axis1; }
            set { SetProperty(ref pos_Axis1, value); }
        }
        private double pos_Axis2;
        public double Pos_Axis2
        {
            get { return pos_Axis2; }
            set { SetProperty(ref pos_Axis2, value); }
        }
        private double pos_Axis3;
        public double Pos_Axis3
        {
            get { return pos_Axis3; }
            set { SetProperty(ref pos_Axis3, value); }
        }
        private double pos_Axis4;
        public double Pos_Axis4
        {
            get { return pos_Axis4; }
            set { SetProperty(ref pos_Axis4, value); }
        }
        private double pos_Axis5;
        public double Pos_Axis5
        {
            get { return pos_Axis5; }
            set { SetProperty(ref pos_Axis5, value); }
        }
        private double pos_Axis6;
        public double Pos_Axis6
        {
            get { return pos_Axis6; }
            set { SetProperty(ref pos_Axis6, value); }
        }
        private double pos_Axis7;
        public double Pos_Axis7
        {
            get { return pos_Axis7; }
            set { SetProperty(ref pos_Axis7, value); }
        }
        private double pos_Axis8;
        public double Pos_Axis8
        {
            get { return pos_Axis8; }
            set { SetProperty(ref pos_Axis8, value); }
        }
        #endregion
        #region 构造函数
        public CurrentPosViewModel(IEventAggregator _eventAggregator)
        {
            _eventAggregator.GetEvent<ShellSendEvent>().Subscribe(ShellSendEventReceived);
        }
        #endregion
        #region 自定义函数
        private void ShellSendEventReceived(string msg)
        {
            switch (msg)
            {
                case "Loaded":
                    Run();
                    break;
                default:
                    break;
            }
        }
        #endregion
        #region 运行
        async void Run()
        {
            while (true)
            {            
                try
                {
                    if (DmcCard.Instance.OpenCardOk)
                    {
                        Pos_Axis1 = DmcCard.Instance.EncPos_Axis1;
                        Pos_Axis2 = DmcCard.Instance.EncPos_Axis2;
                        Pos_Axis3 = DmcCard.Instance.EncPos_Axis3;
                        Pos_Axis4 = DmcCard.Instance.EncPos_Axis4;
                        Pos_Axis5 = DmcCard.Instance.EncPos_Axis5;
                        Pos_Axis6 = DmcCard.Instance.EncPos_Axis6;
                        Pos_Axis7 = DmcCard.Instance.EncPos_Axis7;
                        Pos_Axis8 = DmcCard.Instance.EncPos_Axis8;
                    }
                }
                catch { }
                await Task.Delay(100);
            }
            
        }
        #endregion
    }
}
